package com.xdq.quartz;

import lombok.extern.slf4j.Slf4j;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.quartz.impl.matchers.GroupMatcher;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.*;
import java.util.stream.Collectors;

import static org.quartz.SimpleScheduleBuilder.*;

/**
 * @author: 徐东强
 * @date: 2018/9/28 下午6:57
 * @description:
 **/
@Component
@Slf4j
public class SchedulerService {

    private final Scheduler scheduler;

    public SchedulerService(Scheduler scheduler) {
        this.scheduler = scheduler;
    }

    public void clearAll() throws SchedulerException {
        this.scheduler.clear();
    }

    public void executeJobAtNow() throws SchedulerException {
        log.info("=====schedule Job======");
        for (int i = 0; i < 60; i++){
            JobDetail job = JobBuilder.newJob(NewJob.class)
                    .withIdentity("job" + i,"group1")
                    .storeDurably()
                    .build();

            Trigger trigger = TriggerBuilder.newTrigger()
                    .withIdentity("trigger" + i, "group1")
                    .startNow()
                    .withSchedule(simpleSchedule()
//                            .withIntervalInSeconds(5)
//                            .repeatForever()
                            .withMisfireHandlingInstructionNextWithExistingCount())
                    .build();

            this.scheduler.scheduleJob(job, trigger);
        }
    }

    public void pauseJob() throws SchedulerException {
        log.info("=====pause Job======");
        this.scheduler.pauseJob(JobKey.jobKey("job1","group1"));
    }

    public void resumeJob() throws SchedulerException {
        log.info("resume job");
        this.scheduler.resumeJob(JobKey.jobKey("job1","group1"));
    }

    public void update() throws SchedulerException {
        log.info("update exist job");
        JobDetail newJob = JobBuilder.newJob(HelloJob.class)
                .withIdentity("job1", "group1")
                .storeDurably()
                .build();

        this.scheduler.addJob(newJob, true);
    }

    public void updateTrigger() throws SchedulerException {
        log.info("update trigger");

        Trigger oldTrigger = this.scheduler.getTrigger(TriggerKey.triggerKey("trigger1", "group1"));

        TriggerBuilder tb = oldTrigger.getTriggerBuilder();

        Trigger newTrigger = tb
                .startNow()
                .withSchedule(simpleSchedule()
                        .withIntervalInSeconds(1)
                        .repeatForever())
                .build();

        this.scheduler.rescheduleJob(oldTrigger.getKey(), newTrigger);
    }

    public JobDetail getOneJob() throws SchedulerException {
        return this.scheduler.getJobDetail(JobKey.jobKey("job1", "group1"));
    }

    public Trigger getOneTrigger() throws SchedulerException {
        return this.scheduler.getTrigger(TriggerKey.triggerKey("trigger1","group1"));
    }

    public List<Trigger> listTriggers() throws SchedulerException {
        return this.scheduler.getTriggerKeys(GroupMatcher.groupContains("group1"))
                .stream()
                .map(triggerKey -> {
                    try {
                        return this.scheduler.getTrigger(triggerKey);
                    } catch (SchedulerException e) {
                        e.printStackTrace();
                    }
                    return null;
                })
                .collect(Collectors.toList());
    }

    public void addJob(String i) throws SchedulerException {
        JobDetail job = JobBuilder.newJob(NewJob.class)
                .withIdentity("job" + i,"group1")
                .build();

        Trigger trigger = TriggerBuilder.newTrigger()
                .withIdentity("trigger" + i, "group1")
                .startNow()
                .withSchedule(simpleSchedule()
                        .withIntervalInSeconds(5)
                        .repeatForever()
                        .withMisfireHandlingInstructionNowWithRemainingCount())
                .build();
        this.scheduler.scheduleJob(job, trigger);
    }
}
